/*
 * A Pythagorean triplet is a set of three natural numbers, a  b  c, for which,
 * a^2 + b^2 = c^2
 * 
 * For example, 3^2 + 4^2 = 9 + 16 = 25 = 5^2.
 * 
 * There exists exactly one Pythagorean triplet for which a + b + c = 1000
 * Find the product abc.
 */
package pj1;

class Problem9 {

	static final int problem = 1000;
	
	public static void main(String[] args) {
		Integer a = 1, b = 1, c = 1;
		while ((a+b+c) != problem) {
			a++;
			
			while (a*a + b*b != c*c) {
				b++;
				
				while ((a+b+c) < problem && (a*a + b*b) != c*c) {
					
					// c is too big, break
					if ((a*a + b*b) < c*c) break;
					c++;
				}
				
				// c is too big, reset
				if ((a+b+c) > problem) c = 1;
				
				// b is too big, break
				if ((a+b+c) > problem) break;
			}
			
			// b is too big, reset
			if ((a+b+c) > problem) b = 1;
		}
		System.out.println(a*b*c);
	}
}
